<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/7
 * Time: 16:19
 */
namespace core;
class Config{
    static $config=null;
    protected static $model='sys';
    public static function set($name,$value=null,$model){
         $model=$model?:self::$model;
        if(!isset(self::$config[$model])){
            self::$config[$model]=[];
        }
        if(is_string($name)){
            if(!strpos($name,'.')){
                self::$config[$model][strtolower($name)]=$value;
            }else{
                $ar=explode(".",$name,2);
                self::$config[$model][strtolower($ar[0])][$ar[1]]=$value;
            }
        }elseif(is_array($name)){
            if(empty($value)){
               return  self::$config[$model]=array_merge(self::$config[$model],array_change_key_case($name));
            }else{
                self::$config[$model][$value]= isset(self::$config[$model][$value])?array_merge(self::$config[$model][$value],array_change_key_case($name)):self::$config[$model][$value]=$name;
                return self::$config[$model][$value];
            }
        }else{
            return self::$config[$model];
        }

    }

    public static function get($key='',$model=''){
        $model=$model?:self::$model;
        if(empty($key) && isset(self::$config[$model])){
            return self::$config[$model];
        }
        if(strpos($key,'.')){
            $ar=explode('.',$key,2);
            return isset(self::$config[$model][$ar[0]][$ar[1]])?self::$config[$model][$ar[0]][$ar[1]]:null;
        }else{
            return isset(self::$config[$model][$key])?self::$config[$model][$key]:null;
        }
    }

    public static function load($file,$name='',$model=''){
        $model=$model?:self::$model;
        if(!isset(self::$config[$model])){
            self::$config[$model]=[];
        }
        if(is_file($file)){
            $type=pathinfo($file,PATHINFO_EXTENSION );
            $name=strtolower($name);
            if($type=='php'){
                $ar= include $file;

                self::set($ar,$name,$model);
            }
        }else{
            return self::$config[$model];
        }
    }

    public static function has($name,$model=''){
        $model=$model?:self::$model;
        if(!strpos($name,'.')){
            return isset(self::$config[$model][strtolower($name)]);
        }else{
            $ar=explode('.',$name,2);
            return isset(self::$config[$model][strtolower($ar[0])][$ar[1]]);
        }
    }

    public static function reset($model=''){
        $model=$model?:self::$model;
        if($model===true){
            slef::$config=[];
        }else{
            self::$config[$model]=[];
        }

    }
}